import { defineStore } from 'pinia';
import type { UserInfo,UserInput ,ErrorType} from '../model/user';
import {userLogin} from '../api/user'
interface StateType{
	unifo:UserInfo|null
}
export const userStore = defineStore('user', {
	// persist: true,
	state: ():StateType => {
		return { unifo:null };
	},
	// 也可以这样定义
	// state: () => ({ count: 0 })
	getters:{
		// isLoggedIn: (state:StateType) => !!state.unifo?.sessionToken	
	},
	actions:{
		async login({username,password}:UserInput){
			if(!username.trim()||!password.trim()){
				uni.showToast({
					title:"账号或密码不能为空",
					icon:'none'
				})
				return
			}
			let title="登录成功"
			try{
				let res =await userLogin(username,password)
				console.log("测试登录",res.data);
				this.unifo={
					...res.data,
					isLogin:true
					}
				uni.switchTab({
					url:"/pages/home/home"
				})
			}catch(e){
				console.log("登录失败",e);
				title=(e as ErrorType).data!.error
			}
			uni.showToast({
				title,
				icon:'none'
			})
		}
	}
});